<?php
require_once('db_function.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>User Authenticate funtions</title>
</head>

<body>

<?php

function register($username,$email,$password)
{
	//register person with database
	//return true or error message
	
	//connect to database
	$conn = db_connect();
	
	//check if username is unique
	$result = $conn->query("select * from regstered_users where username='".$username."'");
	
	if (!$result)
	{
		throw new Exception('Could not excecute query');
	}
	if($result->num_rows>0)
	{
		throw new Exception('That username is taken - go back and choose another one.');
	}
	//if ok put in database
	if(!$result)
	{
		throw new Exception('Could not register you in the database- please try again later.');
		}
		return true;
}

function login($username, $password)
{
	//check username and password with db
	//if yes, return true
	//else throw exception
	
	//connect to db
	$conn = db_connect();
	
	//check if username is unique
	$result= $conn->query("select * from registered_user where username='".$username."' and passwd=sha1('".$password."')");
	
	if(!$result)
	{
		throw new Exception('could not log you in.');
	}
	if($result->num_rows>0)
	{
		return true;

	}
	else
	{
		throw new Exception('Could not log you in.');
	}
}

function check_valid_user()
{
	//check to see if somebody is logged in and notify them if not
	if(isset($_SESSION['valid_user']))
	{
		echo"logged in ass".$_SESSION['valid_user'].".<br />";
	}
	else
	{
		//not logged in
		echo'you are not logged in.<br/>';
		do_html_url('logging_in.php', 'Login');
		exit;
	}
}
?>
</body>
</html>